import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * @Author:Shuna
 * @Date: 2025/10/08 18:08
 * @Description:
 */
public class WeatherDemo {
    private static final String API_URL = "https://restapi.amap.com/v3/weather/weatherInfo";
    private static final String API_KEY = "133ba40bf8c6d7091bda54c89046fa01";
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {
        String city = "140300";//杭州市330100 阳泉市140300
        String extensions = "all"; //base:返回实况天气 all:返回预报天气
        String output = "json"; //可选值：JSON,XML
        String url = API_URL + "?key=" + API_KEY + "&city=" + city + "&extensions=" + extensions + "&output=" + output;

        try {
            String responseBody = HttpClient.newHttpClient()
                    .send(HttpRequest.newBuilder()
                                    .uri(URI.create(url))
                                    .build(),
                            HttpResponse.BodyHandlers.ofString())
                    .body();

            JsonNode root = mapper.readTree(responseBody);

            System.out.println("=== 杭州天气信息 ===\n");

            // 基本状态
            String status = root.path("status").asText();
            if (!status.equals("1")) {
                System.out.println("请求失败: " + root.path("info").asText());
                return;
            }


            JsonNode lives = root.path("lives");
            if (lives.isArray() && !lives.isEmpty()) {
                JsonNode live = lives.get(0);
                System.out.println("🌍 当前位置: " + live.path("province").asText() + " " + live.path("city").asText());
                System.out.println("🌤️  当前天气: " + live.path("weather").asText());
                System.out.println("🌡️  实时温度: " + live.path("temperature").asText() + "°C");
                System.out.println("💨 风向风力: " + live.path("winddirection").asText() + " " + live.path("windpower").asText() + "级");
                System.out.println("💧 空气湿度: " + live.path("humidity").asText() + "%");
                System.out.println("🕒 发布时间: " + live.path("reporttime").asText());
                System.out.println();
            }

            JsonNode forecasts = root.path("forecasts");
            if (forecasts.isArray() && !forecasts.isEmpty()) {
                JsonNode forecastData = forecasts.get(0);
                JsonNode casts = forecastData.path("casts");

                System.out.println("📍 城市信息: " + forecastData.path("city").asText() +
                        " (" + forecastData.path("province").asText() + ")");
                System.out.println("🕒 发布时间: " + forecastData.path("reporttime").asText());

                System.out.println("\n📅 未来天气预报:");
                System.out.println("─────────────────────────────────────────────────────");

                for (JsonNode cast : casts) {
                    System.out.printf("📅 %s 星期%s\n", cast.path("date").asText(), cast.path("week").asText());
                    System.out.printf("   🌞 白天: %s, %s°C, %s风 %s级\n",
                            cast.path("dayweather").asText(),
                            cast.path("daytemp").asText(),
                            cast.path("daywind").asText(),
                            cast.path("daypower").asText());
                    System.out.printf("   🌙 夜间: %s, %s°C, %s风 %s级\n",
                            cast.path("nightweather").asText(),
                            cast.path("nighttemp").asText(),
                            cast.path("nightwind").asText(),
                            cast.path("nightpower").asText());
                    System.out.println();
                }
            } else {
                System.out.println("未找到天气预报数据");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}